home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 132_01.zip / CE6809.C < prev    next >
Text File  |  1993-06-01  |  7KB  |  363 lines

  1. /* This section contains the code generator */
  2. /*
  3.     This version adapted for the 6809 processor
  4.     Jan 1,1982 by ... A. Griggs
  5. */
  6. /*    Jan 23,1982
  7.     1. Deleted calls to ccgchar,ccgint,ccpchar,ccpint.
  8.     2. Used LEAY k,s in getloc() to generate stack offset address
  9.        when a single post index byte may be used.
  10.     3. Eliminated pop() in putstk() along with ccpchar and ccpint
  11.        calls. Used indirect post increment off the stack pointer.
  12. */
  13. /*    Feb 6,1982
  14.     1. Added function addstk() to replace pop()-add() sequence
  15.        with addstk() eliminating redundant pull-push.
  16. */
  17. /*
  18.     March 7,1982
  19.     1. Changed .byte and .word 6809 opcode generations to assembly
  20.        language for real 6809 assembler.
  21. */
  22. #INCLUDE CDEF.H
  23. /* >>>>>> start of cc8 <<<<<<< */
  24. /* */
  25. /* Begin a comment line for the assembler    */
  26. comment()
  27. {
  28.     outbyte(';');
  29. }
  30. /* Print all assembler information before any code is generated */
  31. header()
  32. {    comment();
  33.     outstr("small-c compiler rev 1.1");
  34.     nl();
  35. }
  36. /* Include the runtime package    */
  37. inclrn()
  38. {
  39.     ol(";include 6809run.asm");
  40. }
  41. /* Print any assembler items required after code output */
  42. trailer()
  43. {    ol(";the end");
  44.     outbyte(26);
  45. }
  46. /* Fetch a static memory cell into the primary register */
  47. getmem(sym)
  48.     char *sym;
  49. {
  50.     if((sym[ident]!=pointer)&&(sym[type]==cchar))
  51.         {ot("ldb    ");
  52.         outstr(sym+name);
  53.         nl();
  54.         ol("SEX ");
  55.         }
  56.     else
  57.         {ot("LDD    ");
  58.         outstr(sym+name);
  59.         nl();
  60.         }
  61. }
  62. /* Fetch the address of the specified symbol into the primary register */
  63. getloc(sym)
  64.     char *sym;
  65. {
  66.     int    symval;
  67. /*  #### note: the following assumes 8080 style byte storage !
  68.     low byte before high byte. If porting this compiler to 
  69.     a machine which does this differently (ie,6800,6809)
  70.     change offset to offset+1 and vice versa.
  71.     It's the COMPILE time environment that calls the tune. Cross
  72.     compilers running on 8080 based systems need not change.
  73. */
  74.     symval= ((sym[offset]&255)+((sym[offset+1]&255)<<8))-sp ;
  75.     if( absolute(symval) < 16) {
  76.         ot("LEAY     ");
  77.         outdec(symval); /* output indexed postbyte */ 
  78.         outstr(",S");
  79.         nl();
  80.         ol("TFR    Y,D");
  81.         }
  82.     else {
  83.     ol("TFR    S,D");
  84.     ot("ADDD    #");
  85.     outdec(symval);
  86.     nl();
  87.     }
  88. }
  89. /*
  90.     absolute returns the absolute of the number passed to it
  91. */
  92. absolute(num)
  93. int num;
  94. {
  95.     if(num < 0) return( -num);
  96.     else       return(num);
  97. }
  98. /* Store the primary register into the specified static memory cell */
  99. putmem(sym)
  100.     char *sym;
  101. {
  102.     if((sym[ident]!=pointer)&&(sym[type]==cchar))
  103.         {ot("STA    ");
  104.         }
  105.     else {ot("STD    ");
  106.          }
  107.     outstr(sym+name);
  108.     nl();
  109.     }
  110. /* Store the specified object type in the primary register at the */
  111. /* address on the top of the stack                  */
  112. putstk(typeobj)
  113.     char typeobj;
  114. {
  115.     if(typeobj==cchar) {
  116.         ol("STB    [,S++]");
  117.         }
  118.     else ol("STD    [,S++]");
  119.     sp=sp + 2;
  120. }
  121. /* Fetch the specified object type indirect through the primary */
  122. /* register into the primary register                */
  123. indirect(typeobj)
  124.     char typeobj;
  125. {    if(typeobj==cchar)    {
  126.         ol("TFR    D,X");
  127.         ol("LDB    ,X");
  128.         ol("SEX");
  129.     }
  130.     else {
  131.         ol("TFR    D,X");
  132.         ol("LDD    ,X");
  133.          }
  134. }
  135. /* Swap the primary and secondary registers */
  136. swap()
  137. {    ol("EXG    D,X");
  138. }
  139. /* Print partial instruction to get an immediate value into the */
  140. /* primary register                        */
  141. immed()
  142. {    ot("LDD    #");
  143. }
  144. /* Push the primary register onto the stack */
  145. push()
  146. {    ol("PSHS    D");
  147.     sp=sp-2;
  148. }
  149. /* Pop the top of the stack into the primary register        */
  150. pop()
  151. {    ol("PULS    X");
  152.     sp=sp+2;
  153. }
  154. /* Swap the primary register and the top of the stack           */
  155. swapstk()
  156. {    ol("PULS    Y");
  157.     ol("PSHS    D");
  158.     ol("EXG    Y,D");
  159. }
  160. /* Call the specified subroutine name    */
  161. call(sname)
  162.     char *sname;
  163. {
  164.     ot("jsr ");
  165.     outstr(sname);
  166.     nl();
  167. }
  168. /* Return from subroutine    */
  169. ret()
  170. {    ol("rts");
  171. }
  172. /* Perform the subroutine call to the value on the top of the stack */
  173. callstk()
  174. {    ol("JSR    [s++]");
  175.     sp=sp+2;
  176. }
  177. /* Jump to the specified internal label number    */
  178. jump(label)
  179.     int    label;
  180. {    ot("jmp ");
  181.     printlabel(label);
  182.     nl();
  183. }
  184. /* Test the primary register and jump if false to label    */
  185. testjump(label)
  186.     int label;
  187. {    ol("ADDD    #0");
  188.     ol("bne    *+5");
  189.     ot("jmp    ");
  190.     printlabel(label);
  191.     nl();
  192. }
  193. /* Print pseudo-op to define a byte    */
  194. defbyte()
  195. {
  196.     ot("FCB    ");
  197. }
  198. /* Print pseudo-op to define storage     */
  199. defstorage()
  200. {    ot("RMB    ");
  201. }
  202. /* Print pseudo-op to define a word    */
  203. defword()
  204. {    ot("FDB    ");
  205. }
  206. /* Modify the stack pointer to the new value indicated    */
  207. modstk(newsp)
  208.     int    newsp;
  209. {    int k;
  210.     k=newsp-sp;
  211.     if(k==0) return newsp;
  212.     if(absolute(k) < 16) {
  213.         ot("LEAS    ");
  214.         outdec(k);
  215.         outstr(",S");
  216.         nl();
  217.     }
  218.     else
  219.         {
  220.     ot("LEAS    ");
  221.     outdec(k);
  222.     outstr(",S");
  223.     nl();
  224.     }
  225.     return newsp;
  226. }
  227. /* Double the primary register */
  228. doublereg()
  229. {    ol("ASLB ! ROLA");
  230. }
  231. /* Add the primary and secondary registers    */
  232. add()
  233. {    ol("PSHS    X");
  234.     ol("ADDD    ,S++");
  235. }
  236. /*
  237.     add the top of stack value to the primary register
  238.     Added value is removed from the stack.
  239. */
  240. addstk()
  241. {
  242.     ol("ADDD    ,S++");
  243.     sp=sp+2;
  244. }
  245. /* Subtract the primary register from the secondary (result in primary)*/
  246. sub()
  247. {    call("ccsub");
  248. }
  249. /* Multiply the primary and secondary registers    (result in primary) */
  250. mult()
  251. {    call("ccmult");
  252. }
  253. /* Divide the secondary register by the primary    */
  254. /* quotient in primary, remainder in secondary  */
  255. div()
  256. {    call("ccdiv");
  257. }
  258. /* Compute remainder (mod) of secondary register divided by the primary */
  259. mod()
  260. {    div();
  261.     swap();
  262. }
  263. /* Inclusive "or" the primary and the secondary registers */
  264. or()
  265. {    call("ccor");
  266. }
  267. /* Exclusive 'or' the primary and the secondary registers */
  268. xor()
  269. {    call("ccxor");
  270. }
  271. /* 'and' the primary and secondary registers (result in primary) */
  272. and()
  273. {    call("ccand");
  274. }
  275. /* Arithmetic shift right the secondary register number of times in */
  276. /*   primary (results in primary)                     */
  277. asr()
  278. {    call("ccasr");
  279. }
  280. /* Aritmetic left shift , similar to asr */
  281. asl()
  282. {    call("ccasl");
  283. }
  284. /* Form two's complement of primary register    */
  285. neg()
  286. {     call("ccneg");
  287. }
  288. /* Form the one's complement of the primary register */
  289. com()
  290. {
  291.     ol("COMA ! COMB");
  292. }
  293. /* Increment the primary register by one */
  294. inc()
  295. {    ol("ADDD    #1");
  296. }
  297. /* Decrement the primary register by one */
  298. dec()
  299. {    ol("SUBD    #1");
  300. }
  301. /* Following are the conditional operators    */
  302. /* They compare the secondary register against the primary     */
  303. /* register and put a literal 1 in the primary if the condition is */
  304. /* true, otherwise they clear the primary register        */
  305. /* */
  306. /* Test for equality    */
  307. eq()
  308. {    call("cceq");
  309. }
  310. /* Test for not equal    */
  311. ne()
  312. {    call("ccne");
  313. }
  314. /* Test for less than (signed)    */
  315. lt()
  316. {    call("cclt");
  317. }
  318. /* Test for less than or equal to     */
  319. le()
  320. {    call("ccle");
  321. }
  322. /* Test fro greater than (signed)    */
  323. gt()
  324. {    call("ccgt");
  325. }
  326. /* Test for greater than or equal to (signed)    */
  327. ge()
  328. {    call("ccge");
  329. }
  330. /* Test for less than (unsigned)        */
  331. ult()
  332. {    call("ccult");
  333. }
  334. /* Test for less than or equal to (unsigned)    */
  335. ule()
  336. {    call("ccule");
  337. }
  338. /* Test for greater than (unsigned)        */
  339. ugt()
  340. {    call("ccugt");
  341. }
  342. /* Test for greater than or equal to (unsigned) */
  343. uge()
  344. {    call("ccuge");
  345. }
  346. ");
  347. }
  348.  */
  349. uge()
  350. {    call("ccuge");
  351. }
  352. ;
  353. }
  354. ge()
  355. {    call("ccuge");
  356. }
  357.  
  358. }
  359. cuge");
  360. }
  361. ll("ccuge");
  362. }
  363. all("ccuge")